<?php

class job_model extends Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::Model();
			
		$this->_table = $this->config->item('database_tables');
	}
	
	// Public methods

	public function get_jobs_per_page()
	{
		$this->db->select('value');
		$this->db->where('name', 'jobs_per_page');
			
		$query = $this->db->get($this->_table['option'], 1);
			
		if ($query->num_rows() == 1)
		{
			$row = $query->row_array();
		}
		else
		{
			return 5;
		}	
		return $row['value'];
	}
	
	public function get_jobs_count($lang_id=2)
	{
		$current_date = date('Y-m-d');
		$this->db->select('id');
		$this->db->where('status', '1');
		$this->db->where('job.started_date <=', $current_date);
		$this->db->where('lang_id',$lang_id);	
		$query = $this->db->count_all_results($this->_table['job']);
		return $query;
	}
	
	public function get_jobs($number = 10, $offset = 0,$lang_id = 2)
	{
		$current_date = date('Y-m-d');
		$this->db->select('*');
		$this->db->from($this->_table['job'] . ' job');
		$this->db->join($this->_table['user'] . ' user', 'job.creator_id = user.id');
		$this->db->where('job.status', '1');
		$this->db->where('job.started_date <=', $current_date);
		$this->db->where('job.lang_id',$lang_id);
		$this->db->order_by('job.id', 'DESC');
		$this->db->limit($number, $offset);
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			
			foreach (array_keys($result) as $key)
			{
				//$result[$key]['categories'] = $this->categories->get_categories_by_ids($this->get_post_categories($result[$key]['id']));
				//$result[$key]['comment_count'] = $this->db->where('post_id', $result[$key]['id'])->from($this->_table['comments'])->count_all_results();
			}
			return $result;
		}
	}
	
	public function get_job_by_url($year, $month, $day, $job_url)
	{
		$date = $year . '-' . $month . '-' . $day;
		
		$this->db->select('job.*, user.display_name');
		$this->db->from($this->_table['job'] . ' job');
		$this->db->join($this->_table['user'] . ' user', 'job.creator_id = user.id');
		$this->db->where('job.status', '1');
		$this->db->where('job.job_url', $job_url);
		$this->db->where('job.started_date', $date);
		$this->db->limit(1);	
		$query = $this->db->get();
		
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			return $result;
		}
	}
}

/* End of file user_model.php */
/* Location: ./application/modules/user/models/user_model.php */